package com.miaosha.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

import java.util.List;

/**
 * @author lsq
 * @create 2021-09-29 7:56
 * @description
 * @menu
 **/
public class demo {

    public static final String OPERATION_LOG_2020_01_01 = "operation_log_2020_01_01";
    public static final String ARTICLE_1_DIANZAN = "article:1:dianzan";

    public static void main(String[] args) {
        Jedis jedis = new Jedis("182.42.113.98", 6379);
        String keys = "keys1";
        jedis.set(keys, "value1");
        System.out.println(jedis.get(keys));

        // 1、分布式锁
        String result = jedis.set("lock_test", "value_test", SetParams.setParams().nx());
        System.out.println(result);
        String reResult = jedis.set("lock_test", "value_test", SetParams.setParams().nx());
        System.out.println(reResult);
        jedis.del("lock_test");
        String reResult1 = jedis.set("lock_test", "value_test", SetParams.setParams().nx());
        System.out.println(reResult1);
        jedis.del("lock_test");

        // 2、博客文章
        Long publishBlogResult = jedis.msetnx("article:1:title", "学习redis",
                "article:1:content", "如何学号redis的使用",
                "article:1:author", "中华石杉",
                "article:1:time", "2020-01-01 00:00:00");
        System.out.println(publishBlogResult);
        List<String> blog = jedis.mget("article:1:title", "article:1:content",
                "article:1:author",
                "article:1:time");
        System.out.println(blog);
        String modifyResult = jedis.mset("article:1:title", "修改后-学习redis",
                "article:1:content", "修改后-如何学号redis的使用",
                "article:1:author", "修改后-中华石杉",
                "article:1:time", "2021-01-01 00:00:00");
        List<String> modifyBlog = jedis.mget("article:1:title", "article:1:content",
                "article:1:author",
                "article:1:time");
        System.out.println(jedis.get("article:1:title"));

        // 3、操作日志
        jedis.setnx(OPERATION_LOG_2020_01_01, "");

        for (int i = 0; i < 10; i++) {
            jedis.append(OPERATION_LOG_2020_01_01, "今天的第" + (i + 1) + "条操作日志\n");
        }

        String operationLog = jedis.get(OPERATION_LOG_2020_01_01);
        System.out.println("今天所有的操作日志：\n" + operationLog);

        // 4、点赞
        for (int i = 0; i < 10; i++) {
            jedis.incr(ARTICLE_1_DIANZAN);
        }
        String dianzanCounter = jedis.get(ARTICLE_1_DIANZAN);
        System.out.println("点赞次数：" + dianzanCounter);
        jedis.decr(ARTICLE_1_DIANZAN);
        dianzanCounter = jedis.get(ARTICLE_1_DIANZAN);
        System.out.println("点赞次数：" + dianzanCounter);
    }
}
